﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Text;
using Whir.Common;

public partial class Whir_System_UserControls_Menu_Menu : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadMenu();
        }
    }

    private string _menuFileName = "";

    /// <summary>
    /// 菜单名称
    /// </summary>
    public string MenuFileName
    {
        get
        {
            if (this.ViewState["MenuFileName"] != null)
            {
                _menuFileName = (string)this.ViewState["MenuFileName"];
            }
            return _menuFileName;
        }
        set { this.ViewState["MenuFileName"] = value; }
    }

    /// <summary>
    /// 动态加载XML菜单
    /// </summary>
    private void LoadMenu()
    {
        //定义Xml操作文档
        XmlDocument menuXd = new XmlDocument();

        try
        {
            //加载XML
            menuXd.Load(Server.MapPath(Settings.GetString("MenuPath") + MenuFileName).ToString());

            //获取根结点
            XmlNode xnFather = menuXd.DocumentElement;

            //获取一级菜单结点
            XmlNodeList firstLevelList = xnFather.ChildNodes;

            //菜单客户端字符串
            StringBuilder firstString = new StringBuilder();
            firstString.Append("<ul class=\"menulist\">\n");

            int k = 0;
            for (int i = 0; i < firstLevelList.Count; i++)
            {
                XmlNode xn = firstLevelList[i];

                //构建子菜单ID
                string subMenuID = "ul" + k.ToString();
                string li = string.Empty;
                
                if (xn.ChildNodes.Count > 0)
                {
                    if (CheckSubMenu(xn))
                    {
                        if (xn.Attributes["status"].Value == "show")
                        {
                            li = string.Format(@"<li><a name=""link"" href=""javascript:void(null)"" id=""one{0}"" onmousedown=""setTab('one',{0},{1})"" class=""a1"">{2}</a></li>", (k + 1), firstLevelList.Count, xn.Attributes["title"].Value);
                            li += CreateSubMenu(xn, (k + 1).ToString(), true);
                        }
                        else
                        {
                            li = string.Format(@"<li><a name=""link"" href=""javascript:void(null)"" id=""one{0}"" onmousedown=""setTab('one',{0},{1})"" class=""a2"">{2}</a></li>", (k + 1), firstLevelList.Count, xn.Attributes["title"].Value);
                            li += CreateSubMenu(xn, (k + 1).ToString(), false);
                        }
                    }

                    k++;
                }

                firstString.Append(li);
            }
            firstString.Append("</ul>\n");
            //添加到客户端
            pnlMenu.Controls.Add(new LiteralControl(firstString.ToString()));
        }
        catch
        {
            throw new Exception("加载菜单失败！");
        }
    }

    /// <summary>
    /// 检查下级节点是否都是隐藏状态
    /// </summary>
    /// <returns></returns>
    private bool CheckSubMenu(XmlNode firstXn)
    {
        //得到一级菜单对应的子菜单
        XmlNodeList secondLevelList = firstXn.ChildNodes;

        foreach (XmlNode xn in secondLevelList)
        {
            if (xn.Attributes["visible"].Value == "true")
            {
                return true;
            }
        }

        return false;
    }

    private string CreateSubMenu(XmlNode firstXn, string id, bool visible)
    {
        //得到一级菜单对应的子菜单
        XmlNodeList secondLevelList = firstXn.ChildNodes;

        //菜单客户端字符串
        StringBuilder secondString;

        secondString = new StringBuilder();

        if (visible)
        {
            secondString.Append("<div id='con_one_" + id + "'>\n");
        }
        else
        {
            secondString.Append("<div id='con_one_" + id + "' style=\"display: none;\">");
        }
        secondString.Append("<ul class=\"menulist2\">\n");
        string li = "";
        string url = "";
        foreach (XmlNode xn in secondLevelList)
        {
            if (xn.Attributes["visible"].Value == "true")
            {
                url = xn.Attributes["url"].Value;

                if (url.StartsWith("http://"))
                    li = string.Format("<li><a href=\"{0}\" target=\"_blank\" class=\"a3\">{1}</a></li>\n", xn.Attributes["url"].Value, xn.Attributes["title"].Value);
                else
                    li = string.Format("<li><a href=\"{0}\" target=\"mainframe\" class=\"a3\">{1}</a></li>\n", xn.Attributes["url"].Value, xn.Attributes["title"].Value);

                secondString.Append(li);
            }
        }

        secondString.Append("</ul>\n");
        secondString.Append("</div>\n");
        return secondString.ToString();
    }
}
